#include "operatelogmodel.h"
#include "qdebug.h"

#include <QDateTime>
OperateLogModel * OperateLogModel::m_pInstance = NULL;

OperateLogModel::OperateLogModel(QObject *parent)
    : QAbstractListModel{parent}
{}

OperateLogModel *OperateLogModel::getInstance()
{
    if(m_pInstance == NULL)
        m_pInstance = new OperateLogModel();
    return m_pInstance;
}

int OperateLogModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    return m_data.size();
}

QVariant OperateLogModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || index.row() >= m_data.size() || index.row() < 0)
        return QVariant();
    const OperateLogInfo &info = m_data.at(index.row());
    switch (role) {
    case TimeRole:
        return info.strTime;
    case LogRole:
        return info.strlog;
    default:
        return QVariant();
    }
}

QHash<int, QByteArray> OperateLogModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[TimeRole] = "time";
    roles[LogRole]  = "log";
    return roles;
}

void OperateLogModel::addData(QString strlog)
{
    qDebug()<<"[操作]"<<strlog;
    beginInsertRows(QModelIndex(), m_data.size(), m_data.size());
    m_data.append({QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"), strlog});
    endInsertRows();
}

